package com.xiaozhen.data.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xiaozhen.alarm.handler.AlarmHandler;
import com.xiaozhen.common.core.utils.StringUtils;
import com.xiaozhen.data.config.InfluxDBConnect;
import com.xiaozhen.data.service.ProcessingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * HISTORY
 *
 * @author zhenweisheng
 * @date 2021/10/28 14:02
 * @description:
 */
@Service
public class ProcessingServiceImpl implements ProcessingService {

    @Autowired
    private InfluxDBConnect influxDBConnect;
    @Autowired
    private AlarmHandler alarmHandler;

    @Override
    public void saveJsonData(JSONObject jsonData) {
        //表名
        String tableName = jsonData.remove("tableName").toString();
        // 要建立索引的字段
        String tagsString = jsonData.remove("tags").toString();
        //要删除的字段
        String removeFieldsString = jsonData.remove("removeFields").toString();
        //处理json数据
        JSONArray resultJSONArray = new JSONArray();
        resultJSONArray.add(new JSONObject());
        JSONArray fields = setFields(jsonData, resultJSONArray);
        int size = fields.size();
        List<Map<String, Object>> list = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            list.add(fields.getJSONObject(i));
        }
        //删除不入库的字段和建立索引的字段
        for (int i = 0; i < size; i++) {
            Map<String, Object> fieldMap = list.get(i);
            //告警处理
            alarmHandler.handleJsonData(tableName,fieldMap);
            // tags 是建立索引要查询的字段
            Map<String, String> tags = new HashMap<>();
            if (StringUtils.isNotEmpty(tagsString)) {
                List<String> tagFields = Arrays.asList(tagsString.split(","));
                if (tagFields != null && tagFields.size() > 0) {
                    for (int j = 0; j < size; j++) {
                        String tagField = tagFields.get(j);
                        //建立索引字段
                        tags.put(tagField, fieldMap.get(tagField).toString());
                        //删除字段
                        fieldMap.remove(tagField);
                    }
                }
            }
            if (StringUtils.isNotEmpty(removeFieldsString)) {
                List<String> removeFields = Arrays.asList(removeFieldsString.split(","));
                if (removeFields != null && removeFields.size() > 0) {
                    for (int j = 0; j < size; j++) {
                        //删除字段
                        fieldMap.remove(removeFields.get(j));
                    }
                }
            }
            influxDBConnect.insert(tableName, tags, fieldMap);
        }
    }

    public JSONArray setFields(JSONObject sourceJson, JSONArray resultJSONArray) {
        Set<Map.Entry<String, Object>> entries = sourceJson.entrySet();
        Iterator<Map.Entry<String, Object>> iterator = entries.iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Object> next = iterator.next();
            Object value = next.getValue();
            if (value instanceof JSONObject) {
                setFields((JSONObject) value, resultJSONArray);
            } else if (value instanceof JSONArray) {
                JSONArray values = (JSONArray) value;
                String jsonString = JSON.toJSONString(resultJSONArray);
                resultJSONArray.clear();
                int size = values.size();
                for (int i = 0; i < size; i++) {
                    JSONObject jsonObject = values.getJSONObject(i);
                    JSONArray jsonArray = setFields(jsonObject, JSON.parseArray(jsonString));
                    for (int j = 0; j < jsonArray.size(); j++) {
                        resultJSONArray.add(jsonArray.getJSONObject(j));
                    }
                }
            } else {
                String key = next.getKey();
                int size = resultJSONArray.size();
                for (int i = 0; i < size; i++) {
                    JSONObject jsonObject = resultJSONArray.getJSONObject(i);
                    jsonObject.put(key, value);
                }
            }
        }
        return resultJSONArray;
    }
}
